package com.star.security.filter;

import org.springframework.util.AntPathMatcher;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;

/**
 * 匿名认证URI
 *
 * @author yangzj
 */
public class AnonymousAuthenticationRequestImpl implements AnonymousAuthenticationRequest {

    private final AntPathMatcher antPathMatcher = new AntPathMatcher();
    private List<String> anonymousUris = Collections.emptyList();

    public AnonymousAuthenticationRequestImpl(List<String> anonymousUris) {
        this.anonymousUris = anonymousUris;
    }

    @Override
    public boolean matches(HttpServletRequest request) {
        if (anonymousUris == null || anonymousUris.isEmpty()) {
            return false;
        }
        String uri = request.getRequestURI();
        String finalUri = uri.substring(uri.indexOf("/", 1));
        return anonymousUris.stream().anyMatch(c -> antPathMatcher.match(c, finalUri));
    }

}
